/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import com.genia.toolbox.basics.editor.gui.AbstractMenuBar;
import com.genia.toolbox.basics.editor.model.document.Element;
import com.genia.toolbox.form.editor.FormEditorController;
import com.genia.toolbox.form.editor.gui.dialog.jspgeneration.JSPGenerationDialog;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.bean.FormInitialSettings;
import com.genia.toolbox.form.editor.model.document.impl.DocumentModel;
import com.genia.toolbox.form.editor.model.form.impl.ContainerFormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;
import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The main menu bar.
 */
@SuppressWarnings("serial")
public class FormEditorMenuBar
    extends AbstractMenuBar<FormEditorGUI, FormEditorController, DocumentModel, FormModel, FormInitialSettings>
{

  /**
   * The add item.
   */
  private JMenuItem itemAddItem = null;

  /**
   * The add form item.
   */
  private JMenuItem itemAddForm = null;

  /**
   * The delete form item.
   */
  private JMenuItem itemDeleteForm = null;

  /**
   * The move up item.
   */
  private JMenuItem itemMoveUp = null;

  /**
   * The move down item.
   */
  private JMenuItem itemMoveDown = null;

  /**
   * The move right item.
   */
  private JMenuItem itemMoveRight = null;

  /**
   * The move left item.
   */
  private JMenuItem itemMoveLeft = null;

  /**
   * The JSP generation button.
   */
  private JMenuItem itemExportToJSP = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public FormEditorMenuBar(FormEditorGUI gui)
  {
    super(gui);
  }



  /**
   * Initialise the specific commands of the menu bar.
   */
  @Override
  public void initialiseSpecificCommands()
  {

    JMenu menuForm = new JMenu(this.getGui().getController().notifyTranslation(Messages.MENUBAR_FORM_TITLE));
    this.add(menuForm);

    this.itemAddItem = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_ITEM_CREATE));
    this.itemAddItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
    URL url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/add.gif");
    this.itemAddItem.setIcon(new ImageIcon(url));
    this.itemAddItem.addActionListener(this);
    menuForm.add(this.itemAddItem);

    this.itemAddForm = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_CREATE));
    this.itemAddForm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/form/editor/image/addForm.gif");
    this.itemAddForm.setIcon(new ImageIcon(url));
    this.itemAddForm.addActionListener(this);
    menuForm.add(this.itemAddForm);

    this.itemDeleteForm = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_DELETE));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/delete.gif");
    this.itemDeleteForm.setIcon(new ImageIcon(url));
    this.itemDeleteForm.addActionListener(this);
    menuForm.add(this.itemDeleteForm);

    menuForm.addSeparator();

    this.itemMoveUp = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_UP));
    this.itemMoveUp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveUp.gif");
    this.itemMoveUp.setIcon(new ImageIcon(url));
    this.itemMoveUp.addActionListener(this);
    menuForm.add(this.itemMoveUp);

    this.itemMoveDown = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_DOWN));
    this.itemMoveDown.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveDown.gif");
    this.itemMoveDown.setIcon(new ImageIcon(url));
    this.itemMoveDown.addActionListener(this);
    menuForm.add(this.itemMoveDown);

    this.itemMoveRight = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_RIGHT));
    this.itemMoveRight.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveRight.gif");
    this.itemMoveRight.setIcon(new ImageIcon(url));
    this.itemMoveRight.addActionListener(this);
    menuForm.add(this.itemMoveRight);

    this.itemMoveLeft = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_LEFT));
    this.itemMoveLeft.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveLeft.gif");
    this.itemMoveLeft.setIcon(new ImageIcon(url));
    this.itemMoveLeft.addActionListener(this);
    menuForm.add(this.itemMoveLeft);

    menuForm.addSeparator();

    this.itemExportToJSP = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.ACTION_JSP_EXPORT));
    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/form/editor/image/eye.gif");
    this.itemExportToJSP.setIcon(new ImageIcon(url));
    this.itemExportToJSP.addActionListener(this);
    menuForm.add(this.itemExportToJSP);

  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    super.actionPerformed(e);
    Object source = e.getSource();
    if (source.equals(this.itemAddItem)) {
      this.getGui().addSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemAddForm)) {
      this.getGui().addSubForm(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemDeleteForm)) {
      this.getGui().deleteSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemMoveUp)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemMoveDown)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemMoveRight)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemMoveLeft)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.itemExportToJSP)) {
      JSPGenerationDialog dialog = new JSPGenerationDialog(this.getGui());
      dialog.setVisible(true);
    }
  }



  /**
   * Refresh the access of the items.
   */
  @Override
  public void refresh()
  {
    super.refresh();
    FormModel model = this.getGui().getSelectedElement();
    if (model == null) {
      this.itemAddItem.setEnabled(false);
      this.itemAddForm.setEnabled(false);
      this.itemDeleteForm.setEnabled(false);
      this.itemMoveUp.setEnabled(false);
      this.itemMoveDown.setEnabled(false);
      this.itemMoveRight.setEnabled(false);
      this.itemMoveLeft.setEnabled(false);
    }
    else {
      if (model instanceof ContainerFormModel) {
        ContainerFormModel containerFormModel = (ContainerFormModel) model;
        int nbDisplayedSubForms = containerFormModel.getNbSubForms();
        if (nbDisplayedSubForms >= containerFormModel.getMaxNbSubForms()) {
          this.itemAddItem.setEnabled(false);
          this.itemAddForm.setEnabled(false);
        }
        else {
          this.itemAddItem.setEnabled(true);
          this.itemAddForm.setEnabled(true);
        }
      }
      else {
        this.itemAddItem.setEnabled(false);
        this.itemAddForm.setEnabled(false);
      }
      if (model.isSubElement()) {
        this.itemDeleteForm.setEnabled(true);
        if (LayoutDirection.HORIZONTAL.equals(model.getParentDirection())) {
          this.itemMoveUp.setEnabled(false);
          this.itemMoveDown.setEnabled(false);
          this.itemMoveRight.setEnabled(true);
          this.itemMoveLeft.setEnabled(true);
        }
        else {
          this.itemMoveUp.setEnabled(true);
          this.itemMoveDown.setEnabled(true);
          this.itemMoveRight.setEnabled(false);
          this.itemMoveLeft.setEnabled(false);
        }
      }
      else {
        this.itemDeleteForm.setEnabled(false);
        this.itemMoveUp.setEnabled(false);
        this.itemMoveDown.setEnabled(false);
        this.itemMoveRight.setEnabled(false);
        this.itemMoveLeft.setEnabled(false);
      }
    }

    Element<?> element = this.getGui().getSelectedElement();
    if (element != null) {
      if (FormType.LABELED.equals(element.getType())) {
        this.getButtonCut().setEnabled(false);
        this.getButtonPaste().setEnabled(false);
      }
      else if (FormType.TAB.equals(element.getType())) {
        this.getButtonPaste().setEnabled(false);
      }
    }
  }

}
